const tree = [
  {
    id: 1,
    title: '研发部',
    pid: null,
    children: [
      { id: 5, title: '前端研发部', pid: 1 },
      { id: 6, title: '后端研发部', pid: 1 },
      { id: 7, title: '算法研发部', pid: 1 }
    ]
  },
  {
    id: 2,
    title: '研发部',
    pid: null,
    children: [
      { id: 5, title: '前端研发部', pid: 2 },
      { id: 6, title: '后端研发部', pid: 2 },
      { id: 7, title: '算法研发部', pid: 2 }
    ]
  },
  {
    id: 3,
    title: '市场部',
    pid: null
  },
  {
    id: 4,
    title: '销售部',
    pid: null
  }
]
function treeToArray(tree) {
  const obj = []
  tree.forEach((item) => {
    if (item.children) {
      obj.push(item, ...item.children)
    } else {
      obj.push(item)
    }
  })
  return obj
}
console.log(treeToArray(tree))
